package com.tyae.dao;

import java.util.List;

import com.tyae.bean.Acao;

public class AcaoDAOHibernate implements AcaoDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<Acao> hibernateDAO = null;

	public AcaoDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getAcaoDAO();
	}
	
	@Override
	public boolean inserir(Acao acao) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.add(acao);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean alterar(Acao acao) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.update(acao);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean excluir(Acao acao) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.remove(acao);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Acao> getSelecionar(String descricao) {
		String 	sql = "FROM Acao as acao WHERE acao.codigoAcao <> 0";

		if (!(descricao.isEmpty())) {
			sql += " AND acao.descricao like '" + descricao + "%' ";  
		}
		
		sql += " ORDER BY acao.descricao";		
 
		List<Acao> lista = hibernateDAO.getSession().createQuery(sql).list();
		
		return lista;
	}	

	@Override
	public Acao getAcao(int codigoAcao) {
		return hibernateDAO.get(codigoAcao);
	}

	@Override
	public List<Acao> getListarTodos() {
		return hibernateDAO.listAll();
	}
	
	@Override
	public boolean isExiste(Acao acao) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isExclusaoLiberada(Acao acao) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Acao> getSelecionar(String descricao, int empresa) {
		String 	sql = "FROM Acao as acao WHERE acao.codigoAcao <> 0";

		if (!(descricao.isEmpty())) {
			sql += " AND acao.descricao like '" + descricao + "%' ";  
		}
		
		sql += "AND acao.empresa = " +empresa; 
			
		sql += " ORDER BY acao.descricao";		
 
		List<Acao> lista = hibernateDAO.getSession().createQuery(sql).list();
		
		return lista;
	}	

}
